import './style.css'
import 'bootstrap'
import $ from 'jquery'


$(document).ready(function () {

    // 根据当前时间判断是白天还是夜晚
    (function () {
        let hour = new Date().getHours();
        if (hour >= 6 && hour < 18) {
            // 白天
            $("div.sun").removeClass("invisible");
            $("div.moon").addClass("invisible");
        } else {
            // 夜晚
            $("div.sun").addClass("invisible");
            $("div.moon").removeClass("invisible");
        }
    })();

    // 更新日期时间
    (function () {
        function updateDateTime() {
            const now = new Date();
            const dateTimeString = now.toLocaleDateString('zh-CN', {
                weekday: 'long',
                year: 'numeric',
                month: 'long',
                day: 'numeric'
            }) + ' ' + now.toLocaleTimeString();
            $("#date-time").text(dateTimeString);
        }

        updateDateTime();
        setInterval(updateDateTime, 1000);
    })();

    // 获取天气信息（假设你有 API Key）
    (function () {
        // $.getJSON("https://api.qweather.com/v7/weather/now?location=101190101&key=你的API_KEY", function (data) {
        //     if (data.now) {
        //         $("#weather").html(`${data.now.text} ${data.now.temp}°C 湿度:${data.now.humidity}%`);
        //     }
        // });
    })();


    // 获取 IP 和位置信息
    (function () {
        $.getJSON("http://ip-api.com/json/?lang=zh-CN", function (data) {
            $("#location").text(`${data.regionName} - ${data.city}`);
        });
    })();

    //一言
    (function () {
        $.ajax({
            url: 'https://v2.jinrishici.com/one.json',
            dataType: "json",
            success: function (data) {
                let content: string = data['data']['content'];
                $('.nutshell').text(content);
            }
        });
    })();

    // 获取系统信息
    (async function () {
        const info = getSystemInfo();
        $("#system-info").text(`${info.os} - ${info.browser}`);
    })();
});


// 获取系统信息
function getSystemInfo() {
    // 获取用户代理字符串
    const userAgent = window.navigator.userAgent;
    let browser = "Unknown";
    let os = "Unknown";
    let version = "";

    // 检测浏览器
    if (userAgent.indexOf("Chrome") > -1) {
        browser = "Google Chrome";
        version = userAgent.match(/Chrome\/([\d.]+)/)?.[1] || "";
    } else if (userAgent.indexOf("Firefox") > -1) {
        browser = "Mozilla Firefox";
        version = userAgent.match(/Firefox\/([\d.]+)/)?.[1] || "";
    } else if (userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") === -1) {
        browser = "Apple Safari";
        version = userAgent.match(/Version\/([\d.]+)/)?.[1] || "";
    } else if (userAgent.indexOf("MSIE") > -1 || userAgent.indexOf("Trident") > -1) {
        browser = "Internet Explorer";
        version = userAgent.match(/(?:MSIE |rv:)(\d+\.\d+)/)?.[1] || "";
    }

    // 检测操作系统
    if (userAgent.indexOf("Windows") > -1) {
        os = "Windows";
        const ntVersion = userAgent.match(/Windows NT (\d+\.\d+)/)?.[1];
        if (ntVersion) {
            switch (ntVersion) {
                case "10.0":
                    os += " 10/11";
                    break;
                case "6.3":
                    os += " 8.1";
                    break;
                case "6.2":
                    os += " 8";
                    break;
                case "6.1":
                    os += " 7";
                    break;
            }
        }
    } else if (userAgent.indexOf("Mac OS") > -1) {
        os = "Mac OS";
        version = userAgent.match(/Mac OS X ([\d_]+)/)?.[1].replace(/_/g, ".") || "";
    } else if (userAgent.indexOf("Linux") > -1) {
        os = "Linux";
    } else if (userAgent.indexOf("Android") > -1) {
        os = "Android";
        version = userAgent.match(/Android ([\d.]+)/)?.[1] || "";
    } else if (userAgent.indexOf("iPhone") > -1 || userAgent.indexOf("iPad") > -1) {
        os = "iOS";
        version = userAgent.match(/OS ([\d_]+)/)?.[1].replace(/_/g, ".") || "";
    }

    // 返回结果对象
    return {
        browser: browser,
        browserVersion: version,
        os: os,
        fullInfo: `${browser} ${version} on ${os}`
    };
}